package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dto.ClazzQueryParam;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;
    @Override
    public Integer add(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        return clazzMapper.add(clazz);
    }

    @Override
    public List<Clazz> findAll() {
        return clazzMapper.findALl();
    }

    @Override
    public PageResult page(ClazzQueryParam clazzQueryParam) {
        PageHelper.startPage(clazzQueryParam.getPage(),clazzQueryParam.getPageSize());
        List<Clazz> list = clazzMapper.list(clazzQueryParam);
        LocalDate localDate = LocalDate.now();
        for (Clazz clazz : list) {
            if (clazz.getBeginDate().isAfter(localDate)){
                clazz.setStatus("未开班");
            } else if (clazz.getEndDate().isBefore(localDate)) {
                clazz.setStatus("已结课");
            }else {
                clazz.setStatus("已开班");
            }
        }
        Page<Clazz> clazzList = (Page<Clazz>) list;
        return new PageResult(clazzList.getTotal(),clazzList.getResult());
    }

    @Override
    public void deleteById(Integer id) {
        clazzMapper.deleteById(id);
    }

    @Override
    public Clazz getById(Integer id) {
        return clazzMapper.getById(id);
    }

    @Override
    public void update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }
}
